package com.nvwa.stater.logback.preparam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class LogPreparamConfig implements WebMvcConfigurer {
    //主程序中提供实现类
    @Autowired
    private LogPreparamProvider logPreparamProvider;

    //如果主程序中没有实现类执行默认实现
    @Bean
    @ConditionalOnMissingBean(LogPreparamProvider.class)
    public LogPreparamProvider logPreparamProvider() {
        return new LogPreparamProvider.DefProvider();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        LogPreparamInterceptor interceptor = new LogPreparamInterceptor(logPreparamProvider);
        registry.addInterceptor(interceptor).order(InterceptorOrder.PREPARAM_INTERCEPTOR_ORDER);
    }
}


